package com.wwdx.controller.rest;

import com.google.common.collect.Lists;
import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxBookClassifyModel;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.dto.BookBusinessDTO;
import com.wwdx.model.dto.HomeBean;
import com.wwdx.model.dto.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.SortedMap;

/**
 * @author liguoliang
 * @date 2017-8-9
 */
@RestController
@RequestMapping("/home")
@Api(tags = "首页接口")
public class RestHomeController extends BaseController {

    /**
     * 首页接口
     */
    @RequestMapping("/index/{token}/{sign}/{timestamp}/{merchantId}")
    @ApiOperation(value = "首页",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "timestamp",value = "时间戳",required = true),
            @ApiImplicitParam(name = "merchantId",value = "商户ID",required = true)
    })
    public ResultBean index(@PathVariable("token") String token, @PathVariable("sign") String sign,
                            @PathVariable("timestamp") String timestamp,@PathVariable("merchantId") String merchantId){
        SortedMap<String, Object> sortedMap = super.getSortedMap(token, timestamp);
        sortedMap.put("merchantId",merchantId);
        ResultBean resultBean = super.validSignResult(sign,sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String openId = wwdxWeixinUserService.getByToken(token);
        HomeBean homeBean = new HomeBean();
        //轮播图
        homeBean.setBannerList(wwdxBannerService.banner());
        WwdxThemeModel wwdxThemeModel = WwdxThemeModel.builder().ishost(1).type(0).status(0).build();
        //精品包
        homeBean.setThemeList2(productService.themeList(wwdxThemeService.selectModelList(wwdxThemeModel,1, 10), openId,merchantId));
        //推荐书单
        wwdxThemeModel.setType(1);
        homeBean.setThemeList(productService.themeList(wwdxThemeService.selectModelList(wwdxThemeModel, 1, 10), openId,merchantId));
        //小课
        WwdxBookModel wwdxBookModel = WwdxBookModel.builder().ishost(1).type(1).status(0).build();
        homeBean.setBookList3(productService.bookList(wwdxBookService.selectModelList(wwdxBookModel,1,10), openId,merchantId));
        //每日听书
        wwdxBookModel.setType(0);
        homeBean.setBookList(productService.bookList(wwdxBookService.selectModelList(wwdxBookModel,1,1), openId,merchantId));
        //热听好书(随机10本书)
        wwdxBookModel.setIshost(null);wwdxBookModel.setRandom(true);
        homeBean.setBookList2(productService.bookList(wwdxBookService.selectModelList(wwdxBookModel,1,10), openId,merchantId));
        //书籍分类
        homeBean.setBookClassifyList(wwdxBookClassifyService.findBookList(0L));
        return renderSuccess(homeBean);
    }

    /**
     * 书籍分类下的标签及书籍列表
     * @param id 分类ID
     */
    @RequestMapping("/classify/{token}/{sign}/{timestamp}/{id}/{merchantId}")
    @ApiOperation(value = "书籍分类",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "timestamp",value = "时间戳",required = true),
            @ApiImplicitParam(name = "merchantId",value = "商户ID",required = true),
            @ApiImplicitParam(name = "id",value = "分类ID",required = true)
    })
    public ResultBean classify(@PathVariable("token") String token, @PathVariable("sign") String sign,
                               @PathVariable("id") Long id,@PathVariable("timestamp") String timestamp,
                               @PathVariable("merchantId") String merchantId){

        SortedMap<String, Object> sortedMap = super.getSortedMap(token,timestamp);
        sortedMap.put("id",id);
        sortedMap.put("merchantId",merchantId);
        ResultBean resultBean = super.validSignResult(sign,sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String openId = wwdxWeixinUserService.getByToken(token);
        List<WwdxBookClassifyModel> tabList = wwdxBookClassifyService.findBookList(id);
        for (WwdxBookClassifyModel wwdxBookClassifyModel : tabList) {
            wwdxBookClassifyModel.setBookList(this.getBookList(wwdxBookClassifyModel.getId(),openId,merchantId));
        }
        return renderSuccess(tabList);
    }

    private List<BookBusinessDTO.BookListDTO> getBookList(Long id, String openId,String merchantId){
        List<WwdxBookClassifyModel> list = wwdxBookClassifyService.findBookList(id);
        List<WwdxBookModel> bookModels = Lists.newArrayListWithExpectedSize(list.size());
        list.forEach(wwdxBookClassifyModel -> bookModels.add(wwdxBookService.getByBookId(wwdxBookClassifyModel.getBookid())));
        return productService.bookList(bookModels, openId,merchantId);
    }
}